package org.bmind.datasword.utils.setup;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.bmind.datasword.config.dto.DChildDatasetContainer;
import org.bmind.datasword.config.dto.DConfig;
import org.bmind.datasword.config.dto.DMultiAnalisysContainer;
import org.bmind.datasword.config.dto.DParentDatasetContainer;
import org.bmind.datasword.config.dto.DMonoAnalisys;
import org.bmind.datasword.config.dto.DMultiAnalisys;
import org.bmind.datasword.config.dto.DPreProcess;


public class XmlPersistence {
	private final static String dmoPath ="/home/valerio/Buffer/dmo.xml";
	private final static String dmuPath ="/home/valerio/Buffer/dmu.xml";
	private final static String pprPath ="/home/valerio/Buffer/dpp.xml";
	private final static String ddsPath ="/home/valerio/Buffer/dds.xml";
	private final static String dcfPath ="/home/valerio/Buffer/dcf.xml";
	
	public XmlPersistence() {super();}
	
	/**
	 * 
	 * @param dobj
	 * @throws FileNotFoundException
	 * @throws JAXBException
	 */
	public static void dobj2xml(Object dobj) throws FileNotFoundException, JAXBException{
		System.out.println(dobj.getClass().getName());
		
		if (dobj.getClass().getName().equals("org.bmind.datasword.config.dto.DMonoAnalisys")){
			rmo2xml((DMonoAnalisys)dobj);
		} 
		else if (dobj.getClass().getName().equals("org.bmind.datasword.config.dto.DMultiAnalisysContainer")){
			 dmu2xml((DMultiAnalisysContainer) dobj);
		}
		else if (dobj.getClass().getName().equals("org.bmind.datasword.config.dto.DPreProcess")){
			ppr2xml((DPreProcess) dobj);
		}	
		else if (dobj.getClass().getName().equals("org.bmind.datasword.config.dto.DParentDatasetContainer")){
			dds2xml((DParentDatasetContainer) dobj);
		}
		else if (dobj.getClass().getName().equals("org.bmind.datasword.config.dto.DConfig")){
			dcfg2xml((DConfig) dobj);
		}
	}
	
	
	
	/**
	 * Marshal RMonoAnalisys object
	 * @param rmonoanalisys
	 * @throws FileNotFoundException 
	 * @throws JAXBException 
	 */
	public static void rmo2xml(DMonoAnalisys rmonoanalisys) throws FileNotFoundException, JAXBException{
		FileOutputStream fos = new FileOutputStream(dmoPath);
		Marshaller parser = JAXBContext.newInstance(DMonoAnalisys.class).createMarshaller();
		parser.marshal (rmonoanalisys,fos);
	}
	
	/**
	 * Unmarshall RMonoAnalisys object
	 * @return RMonoAnalisys
	 * @throws FileNotFoundException 
	 * @throws JAXBException 
	 */
	public static DMonoAnalisys xml2rmo() throws FileNotFoundException, JAXBException{
		FileInputStream fis = new FileInputStream(dmoPath);
		javax.xml.bind.Unmarshaller parser = JAXBContext.newInstance(DMonoAnalisys.class).createUnmarshaller();
		DMonoAnalisys rmonoanalisys = (DMonoAnalisys)parser.unmarshal (fis);
		return rmonoanalisys;
	}
	
	/**
	 * Marshal RMultiAnalisys object
	 * @param rultianalisys
	 * @throws FileNotFoundException 
	 * @throws JAXBException 
	 */
	public static void dmu2xml(DMultiAnalisysContainer rmultianalisys) throws FileNotFoundException, JAXBException{
		FileOutputStream fos = new FileOutputStream(dmuPath);
		Marshaller parser = JAXBContext.newInstance(DMultiAnalisysContainer.class).createMarshaller();
		parser.marshal (rmultianalisys,fos);
	}
	
	/**
	 * Unmarshall RMultiAnalisys object
	 * @return RMultiAnalisys
	 * @throws FileNotFoundException 
	 * @throws JAXBException
	 */
	public static DMultiAnalisysContainer xml2rmu()throws FileNotFoundException, JAXBException{
		FileInputStream fis = new FileInputStream(dmuPath);
		javax.xml.bind.Unmarshaller parser = JAXBContext.newInstance(DMultiAnalisys.class).createUnmarshaller();
		DMultiAnalisysContainer dMultiAnalisysContainer = (DMultiAnalisysContainer)parser.unmarshal (fis);
		return dMultiAnalisysContainer;
	}
	
	/**
	 * Marshal RPreProcess object
	 * @param rpreprocess
	 * @throws FileNotFoundException 
	 * @throws JAXBException ParentDatasetContainer
	 */
	public static void ppr2xml(DPreProcess rpreprocess) throws FileNotFoundException, JAXBException{
		FileOutputStream fos = new FileOutputStream(pprPath);
		Marshaller parser = JAXBContext.newInstance(DPreProcess.class).createMarshaller();
		parser.marshal (rpreprocess,fos);
	}
	
	/**
	 * Unmarshall RPreProcess object
	 * @return RPreProcess
	 * @throws FileNotFoundException 
	 * @throws JAXBException 
	 */
	public static DPreProcess xml2ppr() throws FileNotFoundException, JAXBException{
		FileInputStream fis = new FileInputStream(pprPath);
		javax.xml.bind.Unmarshaller parser = JAXBContext.newInstance(DPreProcess.class).createUnmarshaller();
		DPreProcess dPreProcess = (DPreProcess)parser.unmarshal (fis);
		return dPreProcess;
		
	}
	
	/**
	 * Marshall RListDataset
	 * @throws FileNotFoundException
	 * @throws JAXBException
	 */
	public static void dds2xml(DParentDatasetContainer dParentDatasetContainer)throws FileNotFoundException, JAXBException{
		FileOutputStream fos = new FileOutputStream(ddsPath);
		Marshaller parser = JAXBContext.newInstance(DParentDatasetContainer.class).createMarshaller();
		parser.marshal (dParentDatasetContainer,fos);
	}
	
	/**
	 * Unmarshall RListDataset object
	 * @param DChildDatasetContainer
	 * @throws FileNotFoundException
	 * @throws JAXBException
	 */
	public static DParentDatasetContainer xml2rld() throws FileNotFoundException, JAXBException{
		FileInputStream fis = new FileInputStream(ddsPath);
		javax.xml.bind.Unmarshaller parser = JAXBContext.newInstance(DParentDatasetContainer.class).createUnmarshaller();
		DParentDatasetContainer dParentDatasetContainer = (DParentDatasetContainer)parser.unmarshal (fis);
		return dParentDatasetContainer;
	}
	
	/**
	 * 
	 * @param dConfig
	 * @throws FileNotFoundException
	 * @throws JAXBException
	 */
	public static void dcfg2xml(DConfig dConfig) throws FileNotFoundException, JAXBException{
		FileOutputStream fos = new FileOutputStream(dcfPath);
		Marshaller parser = JAXBContext.newInstance(DConfig.class).createMarshaller();
		parser.marshal (dConfig,fos);
		
	}
}
